// pages/music/music.js
const innerAudioContext = wx.createInnerAudioContext();  //创建音频对象
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentMusic:'',
    music:[]
  },

  hearHandle: function (e) {
    let that = this
    let { currentMusic, music } = that.data
    music = (music || []).map((v,k)=>{
      if(k == e.currentTarget.id) v.play = true;
      else v.play = false;
      return v;
    })
    let id = e.currentTarget.id
    that.setData({
      music
    })
    // 设置音频地址
    innerAudioContext.src = music[id]['content'];
    console.log(innerAudioContext.src)
    innerAudioContext.play()
  },
  selectuse: function (e) {
    let that = this
    let { currentMusic, music } = that.data
    let id = e.currentTarget.id
    wx.setStorageSync('music', music[id])
    wx.navigateBack({
      changed:true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let that = this;
      wx.request({
        url: app.siteinfo.rootpath+'musiclist',
        success:function(res){
          res.data.list= (res.data.list || []).map((v,k)=>{
            v.play = false;
            return v;
          })
          that.setData({
            music:res.data.list
          })
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    innerAudioContext.stop()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    innerAudioContext.stop()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})